package com.woniu.exception;

import cn.hutool.json.JSONUtil;
import com.woniu.exception.account.AccountException;
import com.woniu.exception.file.FileException;
import com.woniu.exception.order.OrderException;
import com.woniu.exception.permissions.PermissionsException;
import com.woniu.exception.staff.StaffException;
import com.woniu.model.Result;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.MalformedJwtException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.security.SignatureException;
@Component
@RestControllerAdvice
public class GolbalException {

    //算数异常
    @ExceptionHandler(ArithmeticException.class)
    public Result handleArithmeticException(ArithmeticException e){
        return Result.newInstance(String.class).setMessage(e.getMessage()).setCode(500);
    }

    //登录异常
    @ExceptionHandler(AccountException.class)
    public Result handleAccountException(AccountException e){
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage(e.getMessage()).setCode(e.getCode());
    }
    //权限管理异常
    @ExceptionHandler(PermissionsException.class)
    public Result handleAccountException(PermissionsException e){
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage(e.getMessage()).setCode(e.getCode());
    }
    //订单管理异常
    @ExceptionHandler(OrderException.class)
    public Result handleAccountException(OrderException e){
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage(e.getMessage()).setCode(e.getCode());
    }
    //员工异常
    @ExceptionHandler(StaffException.class)
    public Result handleAccountException(StaffException e){
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage(e.getMessage()).setCode(e.getCode());
    }
    //文件类型异常
    @ExceptionHandler(FileException.class)
    public Result handleAccountException(FileException e){
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage(e.getMessage()).setCode(e.getCode());
    }




    //jwt格式异常
    @ExceptionHandler(MalformedJwtException.class)
    public Result MalformedJwtException(MalformedJwtException e){
        String s = JSONUtil.toJsonStr(Result.newInstance(String.class).setMessage("令牌签名异常").setCode(401));
        System.out.println(s);
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage("令牌格式异常").setCode(401);
    }
    //jwt签名异常
    @ExceptionHandler(SignatureException.class)
    public Result SignatureException(SignatureException e){
        String s = JSONUtil.toJsonStr(Result.newInstance(String.class).setMessage("令牌签名异常").setCode(401));
        System.out.println(s);
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage("令牌签名异常").setCode(401);
    }
    //jwt过期异常
    @ExceptionHandler(ExpiredJwtException.class)
    public Result ExpiredJwtException(ExpiredJwtException e){
        String s = JSONUtil.toJsonStr(Result.newInstance(String.class).setMessage("令牌签名异常").setCode(401));
        System.out.println(s);
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage("令牌过期异常").setCode(401);
    }

    //总异常
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        System.out.println("总异常");
        e.printStackTrace();
        return Result.newInstance(String.class).setMessage(e.getMessage()).setCode(500);
    }
}
